import java.beans.Transient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-05-29
 * Time:12:22
 */
public class Test {
    //<a href="//www.qidian.com/book/1036370336/"
    // target="_blank" data-eid="qd_B58"
    // data-bid="1036370336"
    // title="宿命之环最新章节在线阅读">宿命之环</a>
    static String regex = "<a href=\"//www\\.qidian\\.com/book/\\d{10}/\" target=\"\\w{6}\" data-eid=\"\\w{6}\" data-bid=\"\\d{10}\" title=\"[\\u4e00-\\u9fa5]+\">([\\u4e00-\\u9fa5]+)</a>";
    public static void main(String[] args) throws IOException {
        func1();
//        func2();
    }
    public static void func1(){
        String content = "<a href=\"//www.qidian.com/book/1036370336/\" target=\"_blank\" data-eid=\"qd_B58\" data-bid=\"1036370336\" title=\"宿命之环最新章节在线阅读\">宿命之环</a>";
        String regex = "<a href=\"//www\\.qidian\\.com/book/\\d{10}/\" target=\"\\w{6}\" data-eid=\"\\w{6}\" data-bid=\"\\d{10}\" title=\"[\\u4e00-\\u9fa5]+\">([\\u4e00-\\u9fa5]+)</a>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()){
            System.out.println(matcher.group(1));
        }
    }
    public static void func2() throws IOException {
        String link = "http://www.qidian.com/all/";
        URL url = new URL(link);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        while(connection.getResponseCode()==302){
            String location = connection.getHeaderField("Location");
            connection.disconnect();
            link = location;
            connection = (HttpURLConnection) new URL(link).openConnection();
        }
        if(connection.getResponseCode() == 202){
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = null;
            String line = "";
            while((line = br.readLine())!=null){
                System.out.println(line);
                matcher = pattern.matcher(line);
                while(matcher.find()){
                    System.out.println(matcher.group(1));
                }
            }
        }
    }
}
